import {Component, OnInit} from '@angular/core';
import {SubjectModel} from '../model/subject.model';
import {HttpClient} from '@angular/common/http';
import {NewsModel} from '../model/news.model';

@Component({
  selector: 'app-admin',
  templateUrl: './admin.component.html',
  styleUrls: ['./admin.component.scss']
})
export class AdminComponent implements OnInit {
  subjects: SubjectModel[];
  news: NewsModel;
  displayNews: any;
  searchInput: string;

  constructor(private http: HttpClient) {
  }

  ngOnInit() {
    this.http.get('assets/news.json')
      .subscribe((data: any) => {
        this.news = data;
        this.displayNews = JSON.parse(JSON.stringify(data));
      });
  }

  searchContent() {
    this.displayNews.articles = this.news.articles.filter(item => item.content.indexOf(this.searchInput) !== -1);
  }

  filterAuthor() {
    this.displayNews.articles = this.news.articles.filter(item => item.author.indexOf(this.searchInput) === -1);
  }

  reset() {
    this.displayNews.articles = this.news.articles;
    this.searchInput = '';
  }

}
